;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2019 Jelle Licht <jlicht@fsfe.org>
;;;
;;; This file is NOT part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (staging web-extensions)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (gnu packages python)
  #:use-module (gnu packages compression)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system trivial)
  #:use-module (guix git-download)
  #:use-module (guix packages)
  #:use-module (guix download))

(define-public ublock-assets
  (let ((commit "a3535ee934034065790ab394f7e38d1f021b66b7"))
    (package
      (name "ublock-assets")
      (version (git-version "0" "0" commit))
      (source (origin
                (method git-fetch)
                (uri (git-reference
                      (url "https://github.com/uBlockOrigin/uAssets.git")
                      (commit commit)))
                (file-name (git-file-name name version))
                (sha256
                 (base32
                  "1ifzb625pamqq3mxz4m8w7gc3g0dpbsj26z2iwp0g0rpwpsn3xh3"))))
      (build-system trivial-build-system)
      (arguments
       `(#:modules ((guix build utils))
         #:builder (begin
                     (use-modules (guix build utils))
                     (let ((source (assoc-ref %build-inputs "source")))
                       (mkdir-p %output)
                       (copy-recursively source %output)))))
      (home-page "https://github.com/uBlockOrigin/uAssets")
      (synopsis "Filter lists and rulesets for blockers")
      (description
       "uAssets includes resources for uBlock Origin and uMatrix.  It
consists of static filter lists and ready-to-use rulesets.")
      (license license:gpl3+))))

(define-public web-extension-ublock-origin
  (package
    (name "web-extension-ublock-origin")
    (version "1.29.0")
    (source (origin
              (method git-fetch)
              (uri (git-reference
                    (url "https://github.com/gorhill/uBlock.git")
                    (commit version)))
              (file-name (git-file-name name version))
              (sha256
               (base32
                "020c4gl90masdzb8wi0ydfhky74wdwhg5msl38xw33bvy8dwc1gq"))))
    (build-system gnu-build-system)
    (arguments
     `(#:tests? #f
       #:modules ((guix build json)
                  (jlicht build xpi-utils)
                  ,@%gnu-build-system-modules)
       #:imported-modules (,@%gnu-build-system-modules
                           (guix build json)
                           (jlicht build xpi-utils))
       #:phases (modify-phases %standard-phases
                  (add-after 'unpack 'fix-paths
                    (lambda* (#:key inputs outputs #:allow-other-keys)
                      (let ((python (string-append (assoc-ref inputs "python")
                                                   "/bin/python3"))
                            (assets (assoc-ref inputs "ublock-assets"))
                            (out (assoc-ref outputs "out")))
                        (substitute* "tools/make-firefox.sh"
                          (("python")
                           python))
                        (substitute* "tools/make-assets.sh"
                          (("\\.\\./uAssets")
                           assets)))
                      #t))
                  (delete 'configure)
                  (add-before 'build 'make-writable
                    (lambda _
                      (for-each
                       (lambda (file)
                         (chmod file #o644))
                       (find-files "." "."))
                      #t))
                  (replace 'build
                    (lambda* (#:key outputs #:allow-other-keys)
                      (let (
                            (out (assoc-ref outputs "out")))
                        (invoke "sh" "tools/make-firefox.sh" "all"))))
                  (replace 'install
                    (lambda* (#:key outputs #:allow-other-keys)
                      (let* ((out (assoc-ref outputs "out"))
                             (xpi-file (string-append "dist/build/uBlock0.firefox.xpi"))
                             (str (read-manifest-json xpi-file))
                             (id (extract-id str))
                             (extension-file (string-append "dist/build/"
                                                            id ".xpi")))
                        (rename-file xpi-file extension-file)
                        (install-file  extension-file
                                       (string-append out "/extensions"))
                        #t))))))
    (native-inputs
     `(("zip" ,zip)
       ("unzip" ,unzip)
       ("python" ,python)
       ("ublock-assets" ,ublock-assets)))
    (home-page "https://github.com/gorhill/uBlock")
    (synopsis "Efficient blocker implemented as WebExtension")
    (description "uBlock Origin is a wide-spectrum blocker. By default
it blocks ads, trackers and malware sites.")
    (license license:gpl3+)))

web-extension-ublock-origin
